$(function() {
    toastr.options = messageOpts;

    $("#accoutTable").bootstrapTable('destroy').bootstrapTable({
        url : window.portAddress + "/account/getAccountList",
        dataType : "json",
        method : 'get',
        striped: true,  //行间隔色
        cache : false,  //禁用缓存
        pageNumber: 1,  //初始化加载第一页
        pagination: true,//是否分页
        sidePagination: "client",//前端分页
        pageSize: 5,//单页记录数
        pageList: [5, 10, 15],//可选择单页记录数
        clickToSelect : true,
        theadClasses: "thead-pink",
        queryParams: function (params) {
            //向服务器传递的参数
            return {
               /* option : $("#search_option option:selected").val(),*/
                accountNo : $("#searchAccountNo").val(),
                currency: $("#searchCurrency").val(),
                customerNo: $("#searchCustomerNo").val(),
            };
        },
        responseHandler : function(res) {
            return res.data;
        },
        columns : [
            {
                title : "账户号",
                field : "accountNo",
                sortable : true,
                align : "center",
            },
            {
                title : "货币",
                field : "currency",
                align : "center",
            },
            {
                title : "账户额度",
                field : "accountBalance",
                align : "center",
            },
            {
                title : "账户类型",
                field : "accountType",
                align : "center",
            },
            {
                title : "客户号",
                field : "customerNo",
                align : "center",
            },

            {
                title : "备注",
                field : "remark",
                align : "center",
            },
            {
                title : "操作",
                field : "pendingNumber",
                formatter : operation,
                width : 130,
                height: 40,
                align : "center",
            }
        ]
    });

    $("#search").click(function() {
        $.ajax({
            url : window.portAddress + "/account/getAccountList",
            type : "post",
            data : {
                accountNo : $("#searchAccountNo").val(),
                currency: $("#searchCurrency").val(),
                customerNo: $("#searchCustomerNo").val(),
            },
            dataType : "json",
            success : function(data) {
                if (data.state === '000000') {
                    toastr.success("查询数据成功");
                }else {
                    toastr.error(data.message);
                }
                $("#accoutTable").bootstrapTable("refresh");
            }
        })
    });

    function operation(value,row,index) {
        var htm;
        if(value != null){
            return  htm = "<span class=\"badge badge-warning\"><font size='3'>审核中...</font></span>"
        }else{
                htm = "<button type=\"button\" class=\"btn btn-primary\" style='width: 40px;' data-target=\"#updateModel\" data-toggle=\"modal\"><span class=\"glyphicon glyphicon-pencil\"></span></button> "
                    + "<button type=\"button\" class=\"btn btn-danger\" style='width: 40px;' data-target=\"#deleteModel\" data-toggle=\"modal\"><span class=\"glyphicon glyphicon-trash\"></span></button> ";

        }
        return htm;
    }
    var accountBalance;
    var remark;
    $("#updateModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var accountNo = thisBtn.closest('tr').find('td').eq(0).text();
        var currency = thisBtn.closest('tr').find('td').eq(1).text();
        accountBalance = thisBtn.closest('tr').find('td').eq(2).text();
        var accountType = thisBtn.closest('tr').find('td').eq(3).text();
        var customerNo = thisBtn.closest('tr').find('td').eq(4).text();
        remark = thisBtn.closest('tr').find('td').eq(5).text();
        $("#updateAccountNo").val(accountNo);
        $("#updateCurrency").val(currency);
        $("#updateAccountBalance").val(accountBalance);
        $("#updateAccountType").val(accountType);
        $("#updateCustomerNo").val(customerNo);
        $("#updateRemark").val(remark);
    });

    $("#deleteModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var accountNo = thisBtn.closest('tr').find('td').eq(0).text();
        $("#deleteAccountNo").val(accountNo);
    });

    $("#deleteAccount").click(function() {
        $.ajax({
            url : window.portAddress + "/account/deleteAccount",
            type : "post",
            data : {
                accountNo: $("#deleteAccountNo").val(),
                userName: window.sessionStorage.getItem("userName"),
            },
            dataType : "json",
            success : function(data) {
                $("#accoutTable").bootstrapTable("refresh");
                $("#deleteModel").modal('hide');

                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });

    $("#addAccount").click(function () {
        $.ajax({
            url : window.portAddress + "/account/addAccount",
            type : "post",
            data : {
                currency : $("#addCurrency").val(),
                accountType : $("#addAccountType").val(),
                customerNo : $("#addCustomerNo").val(),
                remark : $("#addRemark").val(),
                userName: window.sessionStorage.getItem("userName"),
            },
            dataType : "json",
            success : function(data) {
                $("#accoutTable").bootstrapTable("refresh");
                $("#addModel").modal('hide');

                if (data.state === '000000') {
                    $("#booksTable").bootstrapTable("refresh");
                    $("#addModel").modal('hide');

                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    })

    $("#updateAccount").click(function() {
        //数据校验
        const regex = "/^-?\d+(\.\d{1,2})?$/";
        if(!regex.replace($("#updateAccountBalance").val())){
            return toastr.warning("有效金额为小数点两位！");
        }

        if($("#updateAccountBalance").val() <= 0){
            return toastr.warning("输入金额有误,请重新输入！");
        }

        if($("#updateAccountBalance").val().length > 9 ){
            return toastr.warning("输入金额过大,请重新输入！");
        }

        if($("#updateAccountBalance").val() == accountBalance && $("#updateRemark").val() == remark){
            return toastr.warning("数据未做修改,请进行修改");
        }

        $.ajax({
            url : window.portAddress + "/account/updateAccount",
            type : "post",
            data : {
                accountNo : $("#updateAccountNo").val(),
                currency : $("#updateCurrency").val(),
                accountType : $("#updateAccountType").val(),
                accountBalance : $("#updateAccountBalance").val(),
                remark : $("#updateRemark").val(),
                userName: window.sessionStorage.getItem("userName"),
            },
            dataType : "json",
            success : function(data) {
                $("#accoutTable").bootstrapTable("refresh");
                $("#updateModel").modal('hide');

                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });

    $("#addBtn").click(function () {
        $("#addModel").modal('show');
    });
})
